﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pos.excption;
using System.Collections;
using Spring.Data.NHibernate.Support;
using Spring.Data.NHibernate;
using NHibernate;
using NHibernate.Criterion;

namespace pos.entity
{
    class StockSettle : IHibernateCallback
    {
          private idea.tools.IProgress step;
          public StockSettle(idea.tools.IProgress step)
         {
             this.step = step;
         }
         public object DoInHibernate(ISession session)
        {

     

            t_stock stock = new t_stock();
            SettleTool tool = new SettleTool();

             IList list = stock.List(stock.DC.AddOrder(Order.Asc("seqID")));
              long i = 0;
              step.Init(list.Count);
                foreach (Stock r in list)
                {
                    stock.Init(r);

                    tool.Setttle(stock);
                 //   setSettleMsg(i.ToString());
                    
                    i++;
                   // if(i % 10==0)
                        step.Step(i);
                  //  setSettleMsg(i.ToString());
                }
                //tool.saveall();

             //   setSettleMsg("正在转移数据.....");
                session.Flush();
                
               ISQLQuery sqlqr=session.CreateSQLQuery("insert into t_hstock (select * from t_stock where settleState = 1)");
               sqlqr.ExecuteUpdate();
               step.Over();
               sqlqr = session.CreateSQLQuery("delete from t_stock where settleState=1");
               sqlqr.ExecuteUpdate();
                //i=qr.ExecuteUpdate();

               // db.ExeSql("insert into t_hstock (select * from t_stock where settleState=1 )");
               //session.CreateSQLQuery(
               // IQuery qr = session.CreateQuery("delete from Stock where settleState = :state) ");
                // qr.SetInt16("state", 1);
                //i=qr.ExecuteUpdate();
            ///    db.ExeSql("delete from t_stock where settleState=1 ");

                //transaction.commit();


               // setSettleMsg("结算成功！");
                //db.Close();

        
        

            return true;
        }


    }
}
